package com.simba.server.commands.impl.fetch;

import java.sql.ResultSet;
import com.simba.common.command.ICommand;
import org.apache.log4j.Logger;
import com.simba.common.database.DbOperator;
import com.simba.server.components.SorterParams;
import com.simba.server.components.data.SortInfo;

public class FetchPacketOffByNoUploadCmd implements ICommand
{
	public static final String NAME = "FetchPacketOffByNoUploadCmd";
	protected final static Logger logger = Logger.getLogger(FetchPacketOffByNoUploadCmd.class);
	private final SortInfo sortInfo;
	private final String sql = "select id,barcode,sorterId, boxCode,droppedSlot,errorCodePLC,packetOffTime," +
			"weight,length,width,height,volume,uploadFlag,uploadNum,planId  "
			+ "from tt_packet_off_info where uploadFlag=0 and uploadNum<? order by id asc limit 1;";

	public FetchPacketOffByNoUploadCmd(SortInfo sortInfo)
	{
		this.sortInfo = sortInfo;
	}

	@Override
	public String getName()
	{
		return FetchPacketOffByNoUploadCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		Boolean ret = false;

		if (!(params instanceof DbOperator))
		{
			return ret;
		}

		try
		{
			DbOperator dbOperator = (DbOperator) params;

			dbOperator.prepareStatement(this.sql);
			dbOperator.setInt(1, SorterParams.getInstance().getUploadNum());

			ResultSet rs = dbOperator.executeQuery();

			if (rs.next())
			{
				this.sortInfo.setSortResultId( rs.getInt("id"));
				this.sortInfo.setBarcode(rs.getString("barcode"));
				this.sortInfo.setSorterId(rs.getString("sorterId"));
				this.sortInfo.setDroppedSlotId(rs.getInt("droppedSlot"));
				this.sortInfo.setBoxCode(rs.getString("boxCode"));
				this.sortInfo.setErrorCodePLC(rs.getInt("errorCodePLC"));
				this.sortInfo.setTimePacketOff(rs.getString("packetOffTime"));
				this.sortInfo.setChargeWeight(rs.getString("weight"));
				this.sortInfo.setUploadFlag( rs.getInt("uploadFlag"));
				this.sortInfo.setUploadNum( rs.getInt("uploadNum"));
				this.sortInfo.setLength(rs.getString("length"));
				this.sortInfo.setWidth(rs.getString("width"));
				this.sortInfo.setHeight(rs.getString("height"));
				this.sortInfo.setVolume(rs.getString("volume"));
				this.sortInfo.setPlanId(rs.getString("planId"));
			}

			ret = true;

		}
		catch (Exception ex)
		{
			ret = false;
		}

		return ret;
	}

}
